using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKhachSan.BUS;
using QuanLyKhachSan.DTO;

namespace QuanLyKhachSan
{
    public partial class frmDangNhap : Form
    {
        //thong tin nguoi dang nhap
        private NguoiDungDTO nhanVienDangNhap;

        public NguoiDungDTO NhanVienDangNhap
        {
            get { return nhanVienDangNhap; }
            set { nhanVienDangNhap = value; }
        }
        //bien luu dang nhap thanh cong
        private bool dangNhapThanhCong;

        public bool DangNhapThanhCong
        {
            get { return dangNhapThanhCong; }
            set { dangNhapThanhCong = value; }
        }
        public frmDangNhap()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult kq = MessageBox.Show("Ban co muon thoat khoi chuong trinh hay khong?", "Thong Bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (kq == DialogResult.Yes)
            {
                this.Close();
            }
            
        }
        //kiem tra nhap lieu
        private bool NhapLieuDayDu()
        {
            if (string.IsNullOrEmpty(txtTenDangNhap.Text) == true)
            {
                MessageBox.Show("TEN DANG NHAP KHONG DUOC TRONG");
                txtTenDangNhap.Focus();
                return false;
            }
            else if (string.IsNullOrEmpty(txtMatKhau.Text) == true)
            {
                MessageBox.Show("MAT KHAU KHONG DUOC TRONG");
                txtMatKhau.Focus();
                return false;
            }
            return true;
        }
        //kiem tra login
        private bool KiemTraLogin(string userName, string pass)
        {
           this.nhanVienDangNhap = NguoiDungBUS.ThongTinNguoiDung(userName, pass);
           if (string.IsNullOrEmpty(nhanVienDangNhap.TenDangNhap) == false)
           {
               return true;
           }
           return false;

        }
        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (NhapLieuDayDu())
            {
                string userName = txtTenDangNhap.Text;
                string pass = NguoiDungDTO.MaHoaMD5(txtMatKhau.Text);

                this.dangNhapThanhCong = KiemTraLogin(userName, pass);
                if (this.dangNhapThanhCong == true)
                {
                    this.Close();
                }
                else
                {
                    MessageBox.Show("TEN DANG NHAP hoac MAT KHAU KHONG DUNG!");
                    txtTenDangNhap.Text = "";
                    txtMatKhau.Text = "";
                    txtTenDangNhap.Focus();
                }
            }

        }
    }
}